
/*---数字按键 click 事件调用方法。---*/
//输入数字方法
var numberClick = function (value) {
    var val = document.getElementById("output").value;
    //显示框为0时，输入0无效
    if (value == "0" && val == "0") {
        return;
    };
    if (val == "0") {
        //如果显示框为0, 则去掉0, 只显示输入值
        document.getElementById("output").value = value;
    } else {
        //在显示框显示对应字符
        document.getElementById("output").value = val + value;
    }
}
//输入运算符方法
var operatorClick = function (value) {
    var val = document.getElementById("output").value;
    //判断是否连续输入了两次运算符，运算符后面输入数字，不能连续输入多个运算符
    if (val[val.length - 1] == "") {
        return;
        // console.log("111");
    }
    //在显示框显示对应运算符
    document.getElementById("output").value = val + "" + value + "";
}
//计算并显示结果
var equalClick = function () {
    //分割算术数组
    this.number = document.getElementById("output").value.split("");
    //计算乘除
    for (var index = 0; index < this.number.length; index++) {
        console.log(this.number);
        //若输入的字符最后为 “乘”或“除“ 运算符， 则在最后面加1
        if (this.number[index + 1] == "") {
            this.number[index + 1] = 1;
        }
        if (this.number[index] == "*") {
            //删除数组内已计算数字， 并添加计算后数字
            var index_num = Number(index);
            console.log(index_num);
            var firstNum = Number(this.number[index_num - 1]);
            var secondNum = Number(this.number[index_num + 1]);
            var result = firstNum * secondNum;
            this.number.splice(index_num - 1, 3, result);
        } else if (this.number[index] == "/") {
            //删除数组内已计算数字， 并添加计算后数字	
            var index_num = Number(index);
            var firstNum = Number(this.number[index_num - 1]);
            var secondNum = Number(this.number[index_num + 1]);
            var result = firstNum / secondNum;
            this.number.splice(index_num - 1, 3, result);
        }
    }
    //计算加减
    for (var index = 0; index < this.number.length; index++) {
        if (this.number[index] == "+" || this.number[index] == "-") {
            if (this.number[index] == "+") {
                //删除数组内巳计算数字，并添加计算后数字
                var index_num = Number(index);
                var firstNum = Number(this.number[index_num - 1]);
                var secondNum = Number(this.number[index_num + 1]);
                var result = firstNum + secondNum;
                this.number.splice(index_num - 1, 3, result);
            } else if (this.number[index] == "-") {
                //删除数组内巳计算数字，并添加计算后数字
                var index_num = Number(index);
                var firstNum = Number(this.number[index_num - 1]);
                var secondNum = Number(this.number[index_num + 1]);
                var result = firstNum - secondNum;
                this.number.splice(index_num - 1, 3, result);
            }
        }
    }
    document.getElementById("output").value = this.number[0];
}
//清空计算
//清空数据
var cleanClick = function () {
    document.getElementById("output").value = "";
}
var fn = function () {
    var val = document.getElementById("output").value;
    var reg = new RegExp("^\\d+([+*/-]\\d+)+$");
    if (!reg.test(val)) { //如果验证不通过， 则弹出提示，并置空文本框
        alert("请输入正确的计算表达式");
        document.getElementById("output").value = "";
        return false;
    } else {
        //如果验证通过，则进行计算
        //获取运算符
        var reg1 = /[+*/-]/g;
        var str = (val.match(reg1));
        //获取数字
        var reg2 = /\d+/g;
        var str2 = (val.match(reg2));
        var str1 = [];
        var res = "";
        //在运算符和数字之间加入一个空格符号
        for (var i = 0; i < str.length; i++) {
            str1[i] = "" + str[i] + "";
            res += str2[i] + str1[i];
        }
        var res1 = res + str2[str2.length - 1];
        document.getElementById("output").value = res1;
    }
}

//计算对象
var calculator = {
    //用于保存输入的数字和符号数据
    number: [],
    //计算方法
    numberClick: numberClick,
    operatorClick: operatorClick,
    equalClick: equalClick,
    cleanClick: cleanClick
};